package com.apes.scm.settle.payment.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.metadata.BusinessType;
import com.apes.framework.plugin.soe.store.database.repository.BusinessTypeRepository;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.settle.settle.model.TaxBillItem;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 付款登记明细
 *
 * @author wuq
 * @create 2018-12-25 10:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PaymentRegisterItem.CONST_TABLE_NAME)
public class PaymentRegisterItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "acc_payment_register_item";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付款明细表", prefix = "FI")
    @Comment("付款登记明细编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "paymen_Registert_id", referencedColumnName = "id")
    @Comment("付款单编码")
    private PaymentRegister paymentRegister; /*付款登记主表*/


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_bill_item_id", referencedColumnName = "id")
    @Comment("税票明细编码")
    private TaxBillItem taxBillItem;  //税票明细

    @Column(name = "payable_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("应付款金额")
    private double payableAmount = 0; //应付款金额

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0; //付款金额

    @Comment("源单据")
    @Column(name = "origin", length = 30)
    private String origin;

    @Column(name = "origin_business_type", length = 32)
    @Comment("源单据类型")
    private String originBusinessType;

    @Transient
    private String originBusinessTypeName;

    public String getOriginBusinessTypeName(){
        if (originBusinessType == null) return null;
        BusinessType businessType = SpringManager.getBean(BusinessTypeRepository.class).findOne(originBusinessType);
        if(businessType != null) return businessType.getName();
        return null;
    }

    @Transient
    private String code;    // 税票发票号码
    public String getCode(){
        if(this.taxBillItem != null){
            return this.taxBillItem.getCode();
        }
        return  "";
    }

    @Transient
    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    private Date taxReturnDate;     //税票日期
    public Date getTaxReturnDate(){
        if (this.taxBillItem != null){
            return this.taxBillItem.getTaxReturnDate();
        }
        return null;
    }

    @Transient
    private Date taxBillApproveDate;    // 税票审核日期
    public Date getTaxBillApproveDate(){
        if (this.taxBillItem != null && this.taxBillItem.getTaxBill() != null){
            return this.taxBillItem.getTaxBill().getApproveDate();
        }
        return null;
    }
}
